﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GrouponDesktop.FacturarProveedor
{
    public partial class factProv : Form
    {
        public factProv()
        {
            InitializeComponent();
            dtp_desde.Format = DateTimePickerFormat.Custom;
            dtp_desde.CustomFormat = "yyyy-MM-dd";
            dtp_hasta.Format = DateTimePickerFormat.Custom;
            dtp_hasta.CustomFormat = "yyyy-MM-dd";
        }


        private void b_busc_Click(object sender, EventArgs e)
        {
            ComprarGiftCard.Buscar b = new GrouponDesktop.ComprarGiftCard.Buscar(this.tb_prov, 1);
            b.Show();
        }

        private void tb_prov_TextChanged(object sender, EventArgs e)
        {
            DateTime auxdesde = dtp_desde.Value.Date;
            DateTime auxhasta = dtp_hasta.Value.Date;


            string query = "EXEC CodingSpree.facturarProv '" + auxdesde.ToShortDateString() + "' , '" + auxhasta.ToShortDateString() + "', " + tb_prov.Text;
            BDConnection.FillGrid(query, dgv_fact);

            tb_imp.Text = BDConnection.QueryAndExecuteScalarString("EXEC CodingSpree.impTotal '" + auxdesde.ToShortDateString() + "' , '" + auxhasta.ToShortDateString() + "', " + tb_prov.Text).ToString();
            tb_num.Text = BDConnection.QueryAndExecuteScalarString("EXEC CodingSpree.getNumFact");
        }

        private void b_ref_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tb_prov.Text))
            {
                DateTime auxdesde = dtp_desde.Value.Date;
                DateTime auxhasta = dtp_hasta.Value.Date;


                string query = "EXEC CodingSpree.facturarProv '" + auxdesde.ToShortDateString() + "' , '" + auxhasta.ToShortDateString() + "', " + tb_prov.Text;
                BDConnection.FillGrid(query, dgv_fact);
                tb_imp.Text = BDConnection.QueryAndExecuteScalarString("EXEC CodingSpree.impTotal '" + auxdesde.ToShortDateString() + "' , '" + auxhasta.ToShortDateString() + "', " + tb_prov.Text).ToString();
                tb_num.Text = BDConnection.QueryAndExecuteScalarString("EXEC CodingSpree.getNumFact");

            }
        }

        private void b_fact_Click(object sender, EventArgs e)
        {
            if (dgv_fact.Rows.Count > 0)
            {
                BDConnection.execute("EXEC CodingSpree.insertFact " + this.tb_imp.Text + ", " + this.tb_prov.Text + ", '" + variablesGlobales.Configuracion.fechaHoy + "'");
                for (int i = 0; i < dgv_fact.Rows.Count; i++)
                {
                    BDConnection.execute("EXEC CodingSpree.ItmFact " + dgv_fact.Rows[i].Cells["id_cuponxusuario"].Value.ToString() + " , " + tb_num);
                }
            }
            MessageBox.Show("Proveedor Facturado");
            b_ref.PerformClick();
        
        }


    }
}
